package edu.zut.software.identity.controller;

import edu.zut.software.identity.constant.WorkWXAPI;
import edu.zut.software.identity.utils.HttpClientUtil;
import edu.zut.software.identity.utils.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public final class GetAccessToken {
    /**
     * 获取accessToken失败，打印日志，输出得到json。
     */
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    public static String getAccessToken(){
        String json = HttpClientUtil.get(WorkWXAPI.getGetAccessTokenUrl());
        String accessToken = "";
        Map<String, Object> map = JsonUtil.toMap(json);
        if(Integer.parseInt(map.get("errcode").toString()) == 0){
       //     Map<String, Object> resultMap = (Map<String, Object>) map.get("result");
//            accessToken = resultMap.get("access_token").toString();
            accessToken = map.get("access_token").toString();
        }else{
          // logger.error("get access token error:{}", json);
        }
        return accessToken;
    }

}
